package org.aeon.heroicsagatextedition.commands;

import org.aeon.heroicsagatextedition.constants.CommonlyConstants;
import org.aeon.heroicsagatextedition.exception.GameException;
import org.aeon.heroicsagatextedition.game.GameSettlement;
import org.aeon.heroicsagatextedition.output.OutputFactory;
import org.aeon.heroicsagatextedition.role.product.RoleUnit;

import static org.aeon.heroicsagatextedition.constants.Color.GREEN;
import static org.aeon.heroicsagatextedition.constants.Color.RED;
import static org.aeon.heroicsagatextedition.utils.FontColors.coloring;

/**
 * @projectName: HeroicSagaTextEdition
 * @package: org.aeon.heroicsagatextedition.commands
 * @className: AttackCommand
 * @author: MissingAeon
 * @date: 2023/12/22 13:22
 * @description: 攻击命令类，处理英雄和怪物的攻击操作。
 */
public class AttackCommand implements Command {
    private static AttackCommand command;
    private RoleUnit receiver;
    private int attackPower;

    private AttackCommand() {
    }

    /**
     * 获取一个攻击命令，并设置目标和伤害
     *
     * @return
     */
    public static Command newGoals(RoleUnit receiver, int attackPower) {
        if (command == null) {
            command = new AttackCommand();
        }
        command.receiver = receiver;
        command.attackPower = attackPower;
        return command;
    }


    /**
     * 重置目标
     */
    private void resetTarget() {
        receiver = null;
        attackPower = 0;
    }

    /**
     * 行动
     */
    public void execute() {
        if (receiver == null) {
            throw new GameException("无攻击目标错误！");
        }
        receiver.hpChanged(-attackPower);
        OutputFactory.create().print("--造成了：", coloring(attackPower, RED), "点伤害！");
        if (!receiver.isSurvival()) {
            // 提示目标死亡信息
            OutputFactory.create().print(receiver.getName(), "已被击败！");
            // 目标死亡
            if (receiver.getCamp() == CommonlyConstants.RIGHTEOUS) {
                // 判断是否为英雄 是 游戏结束
                new GameSettlement(false);
            }
        } else {
            OutputFactory.create().print("--", receiver.getName(), "剩余hp：", coloring(receiver.getHp(), GREEN));
        }
        resetTarget();
    }
}
